<?php
/**
 * @file database.php
 * 数据库连接配置
 */
session_start();

class DbCon
{
    private $db_host = 'localhost';
    private $db_type = 'mysql';
    private $db_user = 'root';
    private $db_pass = 'xianghai';
    private $db_name = 'bug';
    private $pdo;
    //关于数据库的链接
    public function __construct()
    {
        $dsn = "mysql:host=" . $this->db_host . ";dbname=" . $this->db_name . ";charset=utf8";
        $db_user = $this->db_user;
        $db_pass = $this->db_pass;
        $this->pdo = new PDO($dsn, $db_user, $db_pass);
    }
    //对于数据库的相关操作
    //增加user用户的数据的写入
    //删除


    public function delete($sql)
    {
        if (isset($_SESSION['name'])) {
            return $this->pdo->exec($sql);
        } else {
            echo "<script>window.location.href='php/view/login.php';</script>";

        }

    }

    //修改
    public function modify($sql)
    {


        if (isset($_SESSION['name'])) {
            return $this->pdo->exec($sql);
        } else {
            echo "<script>window.location.href='php/view/login.php';</script>";

        }
    }

    //查询
    public function query($sql)
    {

        if (isset($_SESSION['id']) && $_SESSION["id"]=="1") {
            $pdoStatement = $this->pdo->query($sql, PDO::FETCH_ASSOC);
            $data = $pdoStatement->fetchAll();
            return $data;
        }
        elseif (isset($_SESSION["name"])){

            $pdoStatement = $this->pdo->query($sql, PDO::FETCH_ASSOC);
            $data = $pdoStatement->fetchAll();
            return $data;
        }
            else
         {
            echo "<script>window.location.href='../view/login.php';</script>";

        }


    }
    //对于成员信息的操作
    //对于成员事件的添加
    public function add($sql)
    {
        if (isset($_SESSION['name'])) {
            $this->pdo->exec($sql);
            return $this->pdo->lastInsertId();
        } else {
            echo "<script>window.location.href='php/view/login.php';</script>";

        }

    }


}




